//#include <iostream.h>
//#include <fstream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <iomanip.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status; //Status 是函数返回值类型，其值是函数结果状态代码。
typedef int ElemType; //ElemType 为可定义的数据类型，此设为int类型

#define MAXSIZE 100			//顺序表可能达到的最大长度
typedef struct {
	char id[20];//ISBN
	char name[120];//书名
	double price;//定价
} Book;
typedef struct {
	Book elem[MAXSIZE]; //存储空间的基地址
	int length; //当前长度
} SqList;

Status InitList_Sq(SqList L) { //算法2.1 顺序表的初始化
	//构造一个空的顺序表L
	Book b;
	/*L.elem = (Book *) malloc(MAXSIZE * sizeof(b));//new Book[MAXSIZE]; //为顺序表分配一个大小为MAXSIZE的数组空间
	if (!L.elem)
		exit(OVERFLOW); //存储分配失败退出*/
	L.length = 0; //空表长度为0
	return OK;
}

Status GetElem(SqList L, int i, Book e) {//算法2.2　顺序表的取值
	if (i < 1 || i > L.length)
		return ERROR; //判断i值是否合理，若不合理，返回ERROR
	e = L.elem[i - 1]; //elem[i-1]单元存储第i个数据元素
	return OK;
}

int LocateElem_Sq(SqList L, double e) { //算法2.3 顺序表的查找
	//顺序表的查找
	for (int i = 0; i < L.length; i++)
		if (L.elem[i].price == e)
			return i + 1;//查找成功，返回序号i+1
	return 0;//查找失败，返回0
}

Status ListInsert_Sq(SqList L, int i, Book e) { //算法2.4 顺序表的插入
	//在顺序表L中第i个位置之前插入新的元素e
	//i值的合法范围是1<=i<=L.length+1
	if ((i < 1) || (i > L.length + 1))
		return ERROR; //i值不合法
	if (L.length == MAXSIZE)
		return ERROR; //当前存储空间已满
	for (int j = L.length - 1; j >= i - 1; j--)
		L.elem[j + 1] = L.elem[j]; //插入位置及之后的元素后移
	L.elem[i - 1] = e; //将新元素e放入第i个位置
	++L.length; //表长增1
	return OK;
}

Status ListDelete_Sq(SqList L, int i) { //算法2.5 顺序表的删除
	//在顺序表L中删除第i个元素，并用e返回其值
	//i值的合法范围是1<=i<=L.length
	if ((i < 1) || (i > L.length))
		return ERROR; //i值不合法
	for (int j = i; j <= L.length; j++)
		L.elem[j - 1] = L.elem[j]; //被删除元素之后的元素前移
	--L.length; //表长减1
	return OK;
}

int main() {
	SqList L;
	int i = 0, temp, a, c, choose;
	double price=0;
	Book e;
	//string head_1, head_2, head_3;
	printf("1. 建立\n");
	printf("2. 输入\n");
	printf("3. 取值\n");
	printf("4. 查找\n");
	printf("5. 插入\n");
	printf("6. 删除\n");
	printf("7. 输出\n");
	printf("0. 退出\n\n");

	choose = -1;
	const char delimiter[] = "\t";
	while (choose != 0) {
		printf("请选择:");
		scanf("%d",&choose);
		switch (choose) {
		case 1://创建顺序表
			if (InitList_Sq(L))
				printf("成功建立顺序表\n\n");
			else
				printf("顺序表建立失败\n\n");
			break;
		case 2: {//顺序表信息输入
			i = 0;
			/*Book books[MAXSIZE];
			L.elem = &books;
			if (!L.elem)
				exit(OVERFLOW);*/
			L.length = 0;
			FILE *fp = fopen("book.txt", "r");
			if (fp==NULL) {
				printf("错误！未找到文件！");
				exit(ERROR);
			}
			char buffer[1000];
			int row=0;
			while (fgets(buffer, 1000, fp) != NULL) {
				//printf("%s", buffer);
				if(row==0){
					row=1;
					continue;
				}
				char *token;

				int seq = 0;
				// 获取第一个子字符串
				token = strtok(buffer, delimiter);

				// 循环获取所有子字符串
				while (token != NULL) {
					printf("%s\n", token);
					// 获取下一个子字符串
					
					if(seq==0){
						strcpy(L.elem[i].id, token);
					}
					if(seq==1){
						strcpy(L.elem[i].name, token);
					}
					if(seq==2){
						L.elem[i].price = atof(token);
					}
					seq++;
					token = strtok(NULL, delimiter);
				}
				printf("token\n");
				//file >> L.elem[i].id >> L.elem[i].name >> L.elem[i].price;
				i++;
			}
			printf("输入 book.txt 信息完毕\n\n");
			L.length = i;
			fclose(fp);
		}
			break;
		case 3://顺序表的取值
			printf("请输入一个位置用来取值：\n");
			scanf("%d",&i);
			temp = GetElem(L, i, e);
			if (temp != 0) {
				printf("查找成功\n");
				printf("第" ,i ,"本图书的信息是：\n");
				printf(e.id, "\t", e.name ,"\t" ,e.price);
			} else
				printf("查找失败！位置超出范围\n\n");
			break;
		case 4: //顺序表的查找
			printf("请输入所要查找价格:");
			scanf("%lf",&price);
			temp = LocateElem_Sq(L, price);
			if (temp != 0) {
				printf("查找成功\n");
				printf("该价格对应的书名为：" , L.elem[temp - 1].name );
			} else
				printf("查找失败！没有这个价格对应的书籍\n\n");
			break;
		case 5: //顺序表的插入
			printf("请输入插入的位置和书本信息，包括：编号 书名 价格（用空格隔开）:");
			//cin >> a;
			scanf("%d",&a);
			scanf("%d",&e.id);
			scanf("%d",&e.name);
			scanf("%d",&e.price);
			//cin >> e.id >> e.name >> e.price; //输入a和b，a代表插入的位置，b代表插入的数值（书本信息）
			if (ListInsert_Sq(L, a, e))
				printf("插入成功.\n\n");
			else
				printf("插入失败.\n\n");
			break;
		case 6: //顺序表的删除
			printf("请输入所要删除的书籍的位置:");
			//cin >> c;
			scanf("%d",&c);
			if (ListDelete_Sq(L, c))
				printf("删除成功.\n\n");
			else
				printf("删除失败.\n\n");
			break;
		case 7: //顺序表的输出
			printf("当前图书系统信息（顺序表）读出:\n");
			for (i = 0; i < L.length; i++)
				printf("%s\t%s\t%.2f\n",L.elem[i].id,L.elem[i].name,L.elem[i].price);
			break;
		}
	}
	return 0;
}